package com.rongxun.hbwallet.ui.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.rongxun.base.BaseActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.scancode.QRCodeSupport;
import com.rongxun.scancode.view.FinderViewStyle2;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;

/**
 * Created by dwq on 2018/3/14/014.
 * e-mail:lomapa@163.com
 */

public class QRCodeScannerActivity extends BaseActivity implements QRCodeSupport.OnScanResultListener, View.OnClickListener {
    private static final String TAG                             = QRCodeScannerActivity.class.getSimpleName();
    private static final int    REQUEST_CODE_QRCODE_PERMISSIONS = 123;
    private static final int    QRCODE_RESULT                   = 124;

    private static final int SCAN_W = 720;
    private static final int SCAN_H = 720;
    @BindView(R.id.scanner_toolsbar)
    Toolbar scannerToolbar;

    private RelativeLayout   rlGallery;
    private RelativeLayout   rlFlashLight;
    private LinearLayout     llBack;
    private String           path;
    private SurfaceView      mSurfaceView;
    private QRCodeSupport    mQRCodeSupport;
    private FinderViewStyle2 mFinderView;

    private boolean isStop = false;

    public int from = NORMAL;

    public static final int NORMAL = 0;
    public static final int HOME   = 1;

    private void initScanner() {

    }

    @Override
    public void onScanResult(String notNullResult, byte[] resultByte) {
        FlyLog.i("qrcode result" + notNullResult);
        switch (from) {
            case HOME:
                ActivityUtils.startActivityWithAnimotion(this,
                        new Intent(this, TransferActivity.class)
                                .putExtra(IntentKey.TO_ADDRESS, notNullResult));
                break;

            default:
                EventUtil.postInfoEvent(EventConf.QRCODE_RESULT, notNullResult);
                break;
        }

        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lly_back:
                finish();
                break;
            case R.id.rl_flash_light:
                mQRCodeSupport.toggleFlashLight();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new RxPermissions(this).request(Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        if (isStop) {
                            isStop = false;
                        }
                        mQRCodeSupport.onResume();
                    } else {
                        finish();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    ToastUtils.showToast(getString(R.string.no_permission));
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
        mQRCodeSupport.onPause();
        isStop = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        //        isStop = true;
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(scannerToolbar, this);
        from = getIntent().getIntExtra(IntentKey.SCAN_FROM, 0);
        rlGallery = findViewById(R.id.rl_gallery);
        rlFlashLight = (RelativeLayout) findViewById(R.id.rl_flash_light);
        llBack = (LinearLayout) findViewById(R.id.lly_back);
        rlGallery.setOnClickListener(this);
        rlFlashLight.setOnClickListener(this);
        llBack.setOnClickListener(this);


        mFinderView = (FinderViewStyle2) findViewById(R.id.capture_viewfinder_view);
        mSurfaceView = (SurfaceView) findViewById(R.id.sufaceview);
        mQRCodeSupport = new QRCodeSupport(mSurfaceView, mFinderView);
        mQRCodeSupport.setScanResultListener(this);


    }

    @Override
    protected int getContentView() {
        return R.layout.activity_qrcode_scanner;
    }
}
